. $topsrcdir/mozconfig-[% IF c("var/osx"); GET 'macos-x86_64'; ELSE; GET c("var/osname"); END; %][% IF c("var/asan") %]-asan[% END %]

[% IF c("var/linux") -%]
  [% IF c("var/linux-cross") %]
    CROSS_COMPILE=1

    CC="clang -B /var/tmp/dist/binutils/bin"
    CXX="clang++ -B /var/tmp/dist/binutils/bin"
    HOST_CC="clang"
    HOST_CXX="clang++"

    export LDFLAGS="-Wl,-rpath-link=/lib/[% c('var/crosstarget') %] -Wl,-rpath-link=/usr/lib/[% c('var/crosstarget') %]"

    ac_add_options --target=[% c('var/crosstarget') %]
    ac_add_options --with-toolchain-prefix=[% c('var/crosstarget') %]-
    mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj-[% c("arch") %]

    # From Mozilla MDN "Compiling 32-bit Firefox on a Linux 64-bit OS" docs
    # Fixes "error: Can't find header fontconfig/fcfreetype.h"
    mk_add_options MOZ_TREE_FREETYPE=1

    # Bug 31448: ld.gold fails if we don't disable debug-symbols
    ac_add_options --disable-debug-symbols

    [% IF c("var/linux-arm") %]
      # Architecture flags from Debian armhf compiler
      ac_add_options --with-arch=armv7-a
      # TODO: Consider enabling NEON here?
      # Debian does not, but Arch Linux ARM does.
      ac_add_options --with-fpu=vfpv3-d16
      ac_add_options --with-float-abi=hard
      ac_add_options --with-thumb=yes

      # From Arch Linux ARM for ESR91
      # https://github.com/archlinuxarm/PKGBUILDs/blob/2242af725c21a82a8e9ecc1d38f1b94cd6ea01ef/extra/firefox/PKGBUILD
      ac_add_options --disable-elf-hack
      ac_add_options --disable-av1
      ac_add_options --enable-optimize="-g0 -O2"
      # One of the following two lines (not sure which) prevents "read-only segment has dynamic relocations" linker error.
      export MOZ_DEBUG_FLAGS=" "
      export RUSTFLAGS="-Cdebuginfo=0"
    [% END %]
  [% ELSE %]
    # We want to build with clang now and point to the GCC toolchain until #29041 is
    # fixed. We explicitly need to define the host compiler as well as for some
    # reason the gcc-toolchain argument does not get passed along otherwise.
    CC="clang --gcc-toolchain=/var/tmp/dist/gcc"
    CXX="clang++ --gcc-toolchain=/var/tmp/dist/gcc"
    HOST_CC=$CC
    HOST_CXX=$CXX

    export BINDGEN_CFLAGS='--gcc-toolchain=/var/tmp/dist/gcc'
  [% END %]
[% END -%]

[% IF c("var/windows") -%]
  CROSS_COMPILE=1

  HOST_CC="clang"
  HOST_CXX="clang++"
  CC="[% c('arch') %]-w64-mingw32-clang"
  CXX="[% c('arch') %]-w64-mingw32-clang++"
  CXXFLAGS="-fms-extensions"
  AR=llvm-ar
  RANLIB=llvm-ranlib

  clang_path=/var/tmp/dist/mingw-w64-clang

  # For Stylo
  BINDGEN_CFLAGS="-I$clang_path/[% c('arch') %]-w64-mingw32/include/c++/v1 -I$clang_path/[% c('arch') %]-w64-mingw32/include"
[% END -%]

[% IF c("var/osx") -%]
  TOOLCHAIN_DIR=/var/tmp/dist/macosx-toolchain

  # ld needs libLTO.so from llvm
  mk_add_options "export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/clang/lib"

  CROSS_CCTOOLS_PATH=$TOOLCHAIN_DIR/cctools
  CROSS_SYSROOT=$TOOLCHAIN_DIR/MacOSX10.12.sdk
  CROSS_PRIVATE_FRAMEWORKS=$CROSS_SYSROOT/System/Library/PrivateFrameworks
  HARDENING_FLAGS="-Werror=format -Werror=format-security -fstack-protector-strong -D_FORTIFY_SOURCE=2"
  FLAGS="-target x86_64-apple-darwin -B $CROSS_CCTOOLS_PATH/bin -isysroot $CROSS_SYSROOT $HARDENING_FLAGS"

  export CC="$TOOLCHAIN_DIR/clang/bin/clang $FLAGS"
  export CXX="$TOOLCHAIN_DIR/clang/bin/clang++ $FLAGS"
  export LLVMCONFIG=$TOOLCHAIN_DIR/clang/bin/llvm-config
  export LDFLAGS="-Wl,-syslibroot,$CROSS_SYSROOT -Wl,-pie"
  export BINDGEN_CFLAGS="$FLAGS"
  export TOOLCHAIN_PREFIX=$CROSS_CCTOOLS_PATH/bin/x86_64-apple-darwin-
  export DSYMUTIL="$TOOLCHAIN_DIR/clang/bin/dsymutil"

  export HOST_CFLAGS="-g"
  export HOST_CXXFLAGS="-g"
  export HOST_LDFLAGS="-g"

  ac_add_options --target=x86_64-apple-darwin
  export MACOS_SDK_DIR=$CROSS_SYSROOT
  export MACOS_PRIVATE_FRAMEWORKS_DIR=$CROSS_PRIVATE_FRAMEWORKS
[% END -%]

[% IF c("var/tor-browser") %]
  ac_add_options --enable-tor-browser-update
  ac_add_options --enable-verify-mar
[% END -%]

[% IF ! c("var/linux-cross") %]
  # Let's make sure no preference is enabling either Adobe's or Google's CDM.
  # TODO: Firefox ESR 91 has a stupid bug. The --enable-eme flag is defined
  # with "when=eme_choices". eme_choices is defined as follows:
  # def eme_choices(target):
  #     if (target.kernel in ('Darwin', 'WINNT', 'Linux') and
  #         target.os not in ('Android', 'iOS') and
  #         target.cpu in ('x86', 'x86_64')):
  #         return ('widevine',)
  #     if target.kernel == 'WINNT' and target.cpu == 'aarch64':
  #         return ('widevine',)
  # Which excludes our linux-cross targets. When the "when" condition for
  # --enable-foo isn't enabled, it's also not enabled for --disable-foo. So
  # using --disable-eme on linux-cross results in the error
  # "mozbuild.configure.options.InvalidOptionError: --disable-eme is not
  # available in this configuration". This means we have to omit --disable-eme
  # on linux-cross. Which means EME will stay disabled... UNTIL Mozilla
  # implements EME for one of our linux-cross targets. At which point EME will
  # silently enable itself in Tor Browser for that target unless the Tor devs
  # carefully audit all merges from upstream to see if they implement EME for
  # one of our linux-cross targets. Someone should report this to Mozilla and
  # get it fixed upstream.
  ac_add_options --disable-eme
[% END %]

ac_add_options --with-relative-profile=[% c('var/ProjectName') %]/Data/Browser
mk_add_options MOZ_APP_DISPLAYNAME="[% c('var/Project_Name') %]"

